package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;

    /**
     * 预约设置
     *
     * @param map 预约设置
     * @return 预约id
     * @throws Exception
     */
    @Override
    public Result order(Map map) throws Exception {
        //1.检查当前日期,判断是否进行了预约设置
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        //根据日期查询预约设置
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        //判断是否设置预约
        if (orderSetting == null) {
            //当前日期不能预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.判断预约日期是否预约已满
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            //预约已满,不能预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //3.检查当前用户是否为会员,根据手机号判断
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);

        //防止重复预约
        if (member != null) {
            //判断会员是否已经预约,已预约则不能重复预约
            Integer memberId = member.getId();
            //套餐id
            int setmealId = Integer.parseInt((String) map.get("setmealId"));

            //根据 会员id 预约时间 套餐id 构造一个预约实体类
            Order order = new Order(memberId, date, setmealId);
            //根据条件查询预约是否存在
            List<Order> list = orderDao.findByConditon(order);
            if (list != null && list.size() > 0) {
                //用户已预约 ,无法完成预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {
            //判断用户是不是会员,需要添加到会员表  共用member,则主键可以共用
            member = new Member();
            member.setName(((String) map.get("name")));
            member.setPhoneNumber(telephone);
            member.setIdCard(((String) map.get("idCard")));
            member.setSex(((String) map.get("sex")));
            member.setRegTime(new Date());
            //添加会员
            memberDao.add(member);
        }

        //4.预约成功,更新当日的已预约人数
        Order order = new Order();
        //设置会员ID
        order.setMemberId(member.getId());
        //预约日期
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        //预约类型
        order.setOrderType(((String) map.get("orderType")));
        //到诊状态
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        //套餐ID
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);

        //5.设置已预约人数+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }


    /**
     * 通过预约id查询 会员名称 套餐名称 预约时间 预约类型
     *
     * @param id 预约id
     * @return 会员名称 套餐名称 预约时间 预约类型
     * @throws Exception
     */
    @Override
    public Map findById(Integer id) throws Exception {

        Map map = orderDao.findById(id);
        if (map != null) {
            //处理日期格式
            Date date = (Date) map.get("orderDate");
            map.put("orderDate", DateUtils.parseDate2String(date));
        }

        return map;
    }


}
